attribute vec3 modelTangent;
attribute vec3 modelBinormal;

varying vec3 viewDirection;
varying vec3 lightDirection;

void main(void)
{
	/* Transform vertices and pass on texture coordinates */
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	gl_TexCoord[0] = gl_MultiTexCoord0 * gl_TextureMatrix[0];
	gl_TexCoord[1] = gl_MultiTexCoord1 * gl_TextureMatrix[1];
	gl_TexCoord[2] = gl_MultiTexCoord2 * gl_TextureMatrix[2];
	
	/* Get view and light directions in viewspace */
	vec3 localViewDirection = -(gl_ModelViewMatrix * gl_Vertex).xyz;
	vec3 localLightDirection = gl_LightSource[0].position.xyz + localViewDirection;
	
	/* Calculate tangent info - stored in attributes */
	vec3 normal = gl_NormalMatrix * gl_Normal;
	vec3 tangent = gl_NormalMatrix * modelTangent;
	vec3 binormal = gl_NormalMatrix * modelBinormal;

	/* Transform localViewDirection into texture space */
	viewDirection.x = dot( tangent, localViewDirection );
	viewDirection.y = dot( binormal, localViewDirection );
	viewDirection.z = dot( normal, localViewDirection );

	/* Transform localLightDirection into texture space */
	lightDirection.x = dot( tangent, localLightDirection );
    lightDirection.y = dot( binormal, localLightDirection );
    lightDirection.z = dot( normal, localLightDirection );
    lightDirection = normalize( lightDirection );
}
